package com.retain.system.service.impl;

import com.retain.system.dto.RoleDTO;
import com.retain.system.dto.UserRoleDTO;
import com.retain.system.mappers.RoleMapper;
import com.retain.system.mappers.UserRoleMapper;
import com.retain.system.service.RoleService;
import com.retain.system.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: LiuWei
 * @date: Create in 16:35 2018/12/27
 */
@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public List<RoleVO> selectRolesByUserId(Long userId) {
        List<RoleDTO> dtos = roleMapper.selectRolesByUserId(userId);
        List<RoleVO> roleVOS = new ArrayList<>(dtos.size());
        dtos.forEach(dto -> roleVOS.add(RoleVO.buildRoleVO(dto)));
        return roleVOS;
    }

    @Override
    public int deleteRUByUserId(Long userId) {
        Example example = new Example(UserRoleDTO.class);
        example.createCriteria().andEqualTo("userId", userId);
        return userRoleMapper.deleteByExample(example);
    }
}
